package io.zerous.engine

import android.app.Activity
import android.graphics.Bitmap
import android.os.Bundle

/**
 * Created by Zerous on 2018/2/3.
 */

abstract class Game : Activity() {

    lateinit var graphics: Graphics
    lateinit var input: Input
    lateinit var files: FileIO
    lateinit var renderView: RenderView

    lateinit var currentScreen: Screen

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        val display = windowManager.defaultDisplay

        val framebuffer = Bitmap.createBitmap(display.width, display.height, Bitmap.Config.RGB_565)

        //Toast.makeText(this, "${display.width}, ${display.height}", Toast.LENGTH_SHORT).show()

        renderView = RenderView(this, framebuffer)
        graphics = Graphics(framebuffer)
        input = Input(this, renderView)
        files = FileIO(this)

        Zex.input = input
        Zex.graphics = graphics
        Zex.files = files

        currentScreen = getScreen()


        setContentView(renderView)
    }

    abstract fun getScreen(): Screen

    fun setScreen(screen: Screen) {
        //currentScreen.pause()
        //currentScreen.dispose()
        screen.resume()
        screen.update(0f)
        currentScreen = screen
    }

    override fun onResume() {
        super.onResume()
        renderView.onResume()
        currentScreen.resume()
    }

    override fun onPause() {
        super.onPause()
        renderView.onPause()
    }
}